const { regClass } = Laya;
import { StaticData } from "../../Data/StaticData";
import { GameRes } from "../../GameRes";
import HttpAPI from "../../HttpManager/HttpAPI";
import HttpManager from "../../HttpManager/HttpManager";
import { EViewType } from "../Data/DataType";
import { EventNames } from "../Data/EventNames";
import { GoodsLis } from "./GoodsLis";
import { ViewExChangeRun } from "./ViewExChangeRun";
import { ViewShopRunBase } from "./ViewShopRun.generated";

@regClass()
export class ViewShopRun extends ViewShopRunBase {
    private arrShopList: Array<any> = [];
    onAwake(): void {
        this.addEvent();
        this.init();
    }
    init() {
        this.createDataList();
        this.PopupCommonTips.visible=false;
    }
    onDestroy(): void {
        this.reomveEvent();
    }
    public showView(): void {
        this.getShopListInfo();
        this.onExchangeGoods(StaticData.integral);
    }
    private getShopListInfo() {
        HttpManager.get(HttpAPI.GET_SHOPPING_LIST, undefined, undefined).then(this.onGetShopList.bind(this)).catch(this.onGetShopList.bind(this));
    }
    private onGetShopList(data: any) {
        this.arrShopList = [];
        if (data && data.success) {
            const result = data.result;
            for (let item of result) {
                this.arrShopList.push({ id: item.id, goodsName: item.name, need: item.integral, img: item.img, status: item.status })
            }
        }
        if (this.arrShopList.length == 0) {
            this.List.visible = false;
            this.NoneState.visible = true;
        }
        else {
            this.NoneState.visible = false;
            this.List.visible = true;
            this.updateShopList();

        }
        this.visible = true;
    }
    private createDataList() {
        this.List.renderHandler = Laya.Handler.create(this, this.onListRender, undefined, false);
        this.List.array = this.arrShopList;
    }
    private updateShopList() {
        this.List.array = this.arrShopList;
        this.List.repeatY = this.arrShopList.length / 4;
        if (this.arrShopList.length < 4) {
            this.List.repeatX = this.arrShopList.length
        }
        else {
            this.List.repeatX = 4;
        }
        this.List.refresh();

        this.List.scrollTo(0);
    }
    onListRender(item: Laya.Box, index: number) {

        if (index > this.List.array.length || index < 0) return;
        let gl = item as GoodsLis;
        const data = this.List.array[index];
        gl.setData(data)
    }
    addEvent() {
        this.BtnClose.on(Laya.Event.CLICK, this, this.onClickClose)
        this.BtnRecord.on(Laya.Event.CLICK, this, this.onClickRecord)
        this.BtnDetail.on(Laya.Event.CLICK, this, this.onClickDetails)
        Laya.stage.on(EventNames.INTEGRAL_CHANGE, this, this.onExchangeGoods)
        Laya.stage.on(EventNames.GAME_OPEN_VIEW, this, this.OpenViews);
        Laya.stage.on(EventNames.POPUP_SHOW_COMMON_TIPS,this,this.showTips);
    }

    reomveEvent() {
        this.BtnClose.off(Laya.Event.CLICK, this, this.onClickClose)
        this.BtnRecord.off(Laya.Event.CLICK, this, this.onClickRecord)
        this.BtnDetail.off(Laya.Event.CLICK, this, this.onClickDetails)
        Laya.stage.off(EventNames.INTEGRAL_CHANGE, this, this.onExchangeGoods)
        Laya.stage.off(EventNames.GAME_OPEN_VIEW, this, this.OpenViews);
        Laya.stage.off(EventNames.POPUP_SHOW_COMMON_TIPS,this,this.showTips);
    }
    onClickClose() {
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        this.visible = false;
    }
    onClickRecord() {
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        let prefab = GameRes.getResPrefab(GameRes.ViewRecord) as Laya.Prefab;
        let obj = prefab.create();
        this.parent.addChild(obj);
    }
    onClickDetails() {
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        let prefab = GameRes.getResPrefab(GameRes.ViewDetail) as Laya.Prefab;
        let obj = prefab.create();
        this.parent.addChild(obj);
    }
    onExchangeGoods(score: number) {
        this.Score.text = score.toString()
        this.getShopListInfo();
    }

    private OpenViews(showView: EViewType) {
        switch (showView) {
            case EViewType.ExChange:
                let prefab1 = GameRes.getResPrefab(GameRes.ViewExChange) as Laya.Prefab;
                let obj1 = prefab1.create() as ViewExChangeRun;
                this.addChild(obj1);
                break;
        }
    }
    showTips(tip:string){
        this.PopupCommonTips.showView(tip);
    }
}